package com.wt.admin.service.ai.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.admin.domain.dto.ai.KnowledgeInfoDTO;
import com.wt.admin.domain.entity.ai.KnowledgeInfoEntity;
import com.wt.admin.domain.vo.ai.KnowledgeInfoVO;
import com.wt.admin.domain.vo.ai.KnowledgeTitleVO;
import com.wt.admin.domain.vo.sys.UserVO;
import com.wt.admin.mapper.ai.KnowledgeInfoMapper;
import com.wt.admin.service.ai.KnowledgeInfoService;
import com.wt.admin.service.file.UploadFileService;
import com.wt.admin.service.vector.Vector;
import com.wt.admin.service.vector.impl.MemoryVectorImpl;
import com.wt.admin.util.FileUtils;
import com.wt.admin.util.PageUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("knowledgeInfo")
@Slf4j
public class KnowledgeInfoServiceImpl extends ServiceImpl<KnowledgeInfoMapper, KnowledgeInfoEntity> implements KnowledgeInfoService, UploadFileService {

    @Resource
    private KnowledgeInfoMapper knowledgeInfoMapper;
    @Autowired
    private Vector vector;


    @Transactional
    @Override
    public void titleDel(Integer id) {
        // 查询所有该标题下的所有文件
        List<KnowledgeInfoEntity> list = knowledgeInfoMapper.findList(id);
        knowledgeInfoMapper.titleDel(id);
        list.forEach(this::delFileAndVector);
    }

    @Override
    public PageUtil.PageVO<KnowledgeInfoVO> infoList(PageUtil.PageDTO<KnowledgeInfoDTO> data) {
        return PageUtil.findPage(data, (page) -> knowledgeInfoMapper.infoList(page, data.getData()));
    }

    @Override
    public KnowledgeInfoVO infoEdit(KnowledgeInfoDTO data) {
        saveOrUpdate(data);
        return BeanUtil.copyProperties(data,KnowledgeInfoVO.class);
    }

    @Override
    public KnowledgeInfoVO infoDel(KnowledgeInfoDTO data) {
        removeById(data.getUrl());
        delFileAndVector(data);
        return null;
    }

    @Override
    public List<KnowledgeInfoVO> infoAll() {
        return knowledgeInfoMapper.infoAll();
    }

    @Override
    public boolean exec(String path, MultipartFile file, Map<String, String[]> map, UserVO userVO) {
        String knowledgeTitleId = map.get("knowledgeTitleId")[0];
        KnowledgeInfoEntity knowledgeInfoEntity = new KnowledgeInfoEntity();
        knowledgeInfoEntity.setUrl(path);
        knowledgeInfoEntity.setKnowledgeTitleId(Integer.valueOf(knowledgeTitleId));
        knowledgeInfoEntity.setName(file.getOriginalFilename());
        save(knowledgeInfoEntity);
        vector.add(FileUtil.file(FileUtils.getImageAbsURL(path)),new HashMap<String, Object>(){
            {
                put("knowledgeTitleId",knowledgeTitleId);
                put("knowledgeInfoId",knowledgeInfoEntity.getUrl());
            }
        });
        return true;
    }

    @Override
    public boolean batchExec(Map<String, MultipartFile> pathMap, Map<String, String[]> map, UserVO userVO) {
        String knowledgeTitleId = map.get("knowledgeTitleId")[0];
        List<KnowledgeInfoEntity> list = pathMap.keySet().stream().map(i -> {
            KnowledgeInfoEntity knowledgeInfoEntity = new KnowledgeInfoEntity();
            knowledgeInfoEntity.setUrl(i);
            knowledgeInfoEntity.setKnowledgeTitleId(Integer.valueOf(knowledgeTitleId));
            MultipartFile file = pathMap.get(i);
            knowledgeInfoEntity.setName(file.getOriginalFilename());
            return knowledgeInfoEntity;
        }).toList();
        saveBatch(list);
        list.forEach(i -> vector.add(FileUtil.file(FileUtils.getImageAbsURL(i.getUrl())),new HashMap<String, Object>(){
            {
                put("knowledgeTitleId",knowledgeTitleId);
                put("knowledgeInfoId",i.getUrl());
            }
        }));
        return true;
    }

    @Override
    public void addFilesToMemory(List<KnowledgeTitleVO> list){
        if(!(vector instanceof MemoryVectorImpl)){
            return;
        }
        log.debug("添加知识库到内存");
        list.forEach(i -> {
            List<KnowledgeInfoEntity> infos = knowledgeInfoMapper.findList(i.getId());
            infos.forEach(j -> vector.add(FileUtil.file(FileUtils.getImageAbsURL(j.getUrl())),new HashMap<String, Object>(){
                {
                    put("knowledgeTitleId",i.getId());
                    put("knowledgeInfoId",j.getUrl());
                }
            }));
        });
    }

    private void delFileAndVector(KnowledgeInfoEntity data) {
        // TODO 删除文件
        vector.deleteByKnowledgeTitleId(data.getKnowledgeTitleId());
    }


}
